package com.culturalCenter.dataCenter.service.impl.site;

import com.culturalCenter.dataCenter.Utils.DateFormatUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.site.MonitoringCountResponse;
import com.culturalCenter.dataCenter.customEntity.site.CustomSiteAlerts;
import com.culturalCenter.dataCenter.entity.site.SiteAlertsMonth;
import com.culturalCenter.dataCenter.mapper.site.SiteAlertsMonthMapper;
import com.culturalCenter.dataCenter.mapper.site.SiteMapper;
import com.culturalCenter.dataCenter.service.site.SiteAlertsMonthService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * SiteAlertsMonth业务层接口实现类
 */
@Service
public class SiteAlertsMonthServiceImpl implements SiteAlertsMonthService {

    @Resource
    private SiteAlertsMonthMapper siteAlertsMonthMapper;
    @Resource
    private SiteMapper siteMapper;

    @Override
    public PageInfo<SiteAlertsMonth> findPage(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> siteAlertsMonthMapper.findPageAll());
    }

    @Override
    public Integer delete(String id) {
        return siteAlertsMonthMapper.deleteById(id);
    }

    @Override
    public Integer update(SiteAlertsMonth siteAlertsMonth) {
        return siteAlertsMonthMapper.update(siteAlertsMonth);
    }

    @Override
    public Integer add(SiteAlertsMonth siteAlertsMonth) {
        return siteAlertsMonthMapper.insert(siteAlertsMonth);
    }

    @Override
    public SiteAlertsMonth findById(String id) {
        return siteAlertsMonthMapper.queryById(id);
    }

    @Override
    public List<SiteAlertsMonth> findAll(SiteAlertsMonth siteAlertsMonth) {
        return (List<SiteAlertsMonth>) siteAlertsMonthMapper.queryAll(siteAlertsMonth);
    }

    @Override
    public MonitoringCountResponse getMonitoringCount(int type, int size, Date date, String siteCode) {
//        String siteId = siteMapper.getSiteIdBySiteCode(siteCode);
        String siteId = siteMapper.selectSiteIdBySiteCode(siteCode);
        MonitoringCountResponse monitoringCountResponse = new MonitoringCountResponse();
        date = (date == null) ? new Date() : date;

        //本月报警/故障_柱状图统计
        int startDate = 1, endDate = size;
        Map<String, Integer> alertsCountMap = new LinkedHashMap<>();
        while (endDate <= 31) {
            String key = (startDate < 10 ? "0" : "") + startDate + "-" + (endDate < 10 ? "0" : "") + endDate;
//            alertsCountMap.put(key, siteAlertsMonthMapper.getCountByDate(type, startDate, endDate, date, siteId));
            alertsCountMap.put(key, siteAlertsMonthMapper.getAlertCountByDate(type, startDate, endDate, siteId));
            startDate += size;
            endDate += size;
        }
        alertsCountMap.put(startDate + "-" + DateFormatUtil.getLastDayByDate(date), siteAlertsMonthMapper.getAlertCountByDate(type, startDate, 31, siteId));
        monitoringCountResponse.setAlertsCountMap(alertsCountMap);

        //本月报警/故障_类型占比统计
        List<CustomSiteAlerts> siteAlertsList = siteAlertsMonthMapper.getTypeCount(type, siteId);
        if (CollectionUtils.isNotEmpty(siteAlertsList)) {
            monitoringCountResponse.setAlertsTypeList(siteAlertsList);
        }
        return monitoringCountResponse;
    }

}
